package be.kuleuven.cs.mop.domain.model.impl;

import be.kuleuven.cs.mop.domain.exceptions.TaskManagerException;


public class Interval{
	
	/**
	 * A class the represents an interval, with a lower and/or upperlimit
	 */
	

	private int lowerLimit;
	private int upperLimit;
	/**
	 * Creates an Interval with given upperlimit and lowerlimit.
	 * @param lowerLimit
	 * @param upperLimit
	 * @throws TaskManagerException.INVALID_LOWERLIMIT_UPPERLIMIT_COMBINATION - The lowerlimit is greater than the upperlimit (or the upperlimit is smaller than the lowerlimit )
	 */
	public Interval(int lowerLimit, int upperLimit) throws TaskManagerException{
		setLowerAndUpperLimit(lowerLimit, upperLimit);
	}
	
	/**
	 * Creates an Interval with either only a upperlimit or only a lowerlimit.
	 * @param limit
	 * @param isUpperLimit
	 * 			
	 * @throws TaskManagerException.INVALID_LOWERLIMIT_UPPERLIMIT_COMBINATION - The lowerlimit is greater than the upperlimit (or the upperlimit is smaller than the lowerlimit )
	 */
	public Interval(int limit, boolean isUpperLimit) throws TaskManagerException{
		if(!isUpperLimit){
			setLowerAndUpperLimit(limit, Integer.MAX_VALUE);
		}
		else{
			setLowerAndUpperLimit(-1, limit);
		}
	}
	
	/**
	 * Returns if the value supplied is contained by this Interval
	 * @param 	value	The value to be checked
	 * @return 	(value >= getLowerLimit() && value <= getUpperLimit())
	 */
	public boolean contains(int value){
		return (satisfiesLowerLimit(value) && satisfiesUpperLimit(value));
	}
	
	/**
	 * Returns if the number to be checked satisfies the upper limit of this Interval
	 */
	public boolean satisfiesUpperLimit(int numberToBeChecked){
		return numberToBeChecked <= getUpperLimit();
	}

	/**
	 * Returns if the number to be checked satisfies the lower limit of this Interval
	 */
	public boolean satisfiesLowerLimit(int numberToBeChecked){
		return numberToBeChecked >= getLowerLimit();
	}
	
	
	/**
	 * Returns the lower limit of this Interval
	 */
	public int getLowerLimit() {
		return lowerLimit;
	}

	/**
	 * Returns the upper limit of this Interval
	 */	
	public int getUpperLimit() {
		return upperLimit;
	}
	
	
	private void setLowerAndUpperLimit(int lowerLimit, int upperLimit) throws TaskManagerException{
		if(lowerLimit <= upperLimit){
			this.lowerLimit = lowerLimit;
			this.upperLimit = upperLimit;
		}
		else{
			throw new TaskManagerException(TaskManagerException.INVALID_LOWERLIMIT_UPPERLIMIT_COMBINATION);
		}
	}
	
}